package com.offdissys.common.redis.config;

import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author ZhangHaiWen
 * @title: RedisConfig
 * @projectName mybatisplustest
 * @description: 字符串
 * @date 2021/11/2411:00
 */
@Configuration
@PropertySource("classpath:redis.properties")
public class RedisConfig {

    @Value("${redis.port}")
    private Integer port;

    @Value("${redis.host}")
    private String host;

    /**
     * RedisTemplate
     * @return
     */
    @Bean(name = "redisTemplate")
    @Scope("prototype")
    public RedisTemplate getRedisTemplate(){
        //单机模式
        RedisStandaloneConfiguration rsc = new RedisStandaloneConfiguration();
        rsc.setPort(port);
        rsc.setHostName(host);
        JedisConnectionFactory fac = new JedisConnectionFactory(rsc);
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(fac);
        return redisTemplate;
    }

    /**
     * Redisson分布式事务锁
     * @return
     */
    @Bean
    public Redisson redisson(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://"+host+":"+port);
        return (Redisson)Redisson.create(config);
    }

}
